const db = require("../config/config");
module.exports = {
  //渲染专区页面
  getZoneinfo(req, res) {
    db.dbMysql(`SELECT * FROM zoneinfo`, [], function (err, data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].zoneShelf == 0) {
          data[i].zoneShelf = true;
        } else {
          data[i].zoneShelf = false;
        }
      }
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: "专区信息查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "服务器错误",
        });
      }
    });
  },

  //删除页面渲染
  deleteid(req, res) {
    db.dbMysql(
      ` DELETE FROM zoneinfo where zoneId=?`,
      [req.body.zoneId],
      function (err, data) {
        if (data) {
          console.log("data", data);
          console.log(res);
          res.send({
            code: 200,
            data: data,
            msg: "删除成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "删除失败",
          });
        }
      }
    );
  },

  //查询语句
  Inquire(req, res) {
    let zoneShelf = req.query.productBrand;


    if (req.query.name == "undefined" && zoneShelf == "undefined") {
      query = "SELECT * FROM zoneinfo";
      params = [];
    } else if (req.query.name == "undefined" && zoneShelf != "undefined") {
      query = "SELECT * FROM zoneinfo where zoneShelf =  ?";
      params = [zoneShelf];
    } else if (req.query.name != "undefined" && zoneShelf == "undefined") {
      query = "SELECT * FROM zoneinfo where zoneName like  ?";
      params = [`%${req.query.name}%`];
    } else if (req.query.name != "undefined" && zoneShelf != "undefined") {
      query =
        "SELECT * FROM zoneinfo where zoneName like  ? AND zoneShelf =  ?";
      params = [`%${req.query.name}%`, zoneShelf];
    }

    db.dbMysql(query, params, function (err, data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].zoneShelf == 0) {
          data[i].zoneShelf = true;
        } else {
          data[i].zoneShelf = false;
        }
      }
      console.log(data, "查询的");
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: "查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "查询失败",
        });
      }
    });
  },

  //编辑
  editable(req, res) {
   
    let zoneName = req.body.row.zoneName;
    let zoneTemplate = req.body.row.zoneTemplate;
    let zoneTimi = req.body.row.zoneTimi;
    let date = new Date(new Date(zoneTimi).getTime() + 8 * 3600 * 1000);
    date = date.toJSON();
    date = date.substring(0, 10);
    console.log(date);
    if (req.body.row.zoneShelf == true) {
      zoneShelf = 0;
    } else {
      zoneShelf = 1;
    }
    let zoneId = req.body.row.dataID;
    db.dbMysql(
      `UPDATE zoneinfo SET zoneName=?, zoneTemplate=?, zoneTimi=?, zoneShelf=? WHERE zoneId=?`,
      [zoneName, zoneTemplate, date, zoneShelf, zoneId],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "修改成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "修改失败",
          });
        }
      }
    );
  },

  //添加
  addTo(req, res) {
 
    db.dbMysql(
      `INSERT INTO zoneinfo(zoneId,zoneName,zoneTemplate,zoneShelf,zoneTimi) VALUES (?,?,?,?,?)`,
      [
        null,
        req.body.zoneName,
        req.body.zoneTemplate,
        req.body.zoneShelf,
        req.body.zoneTimi,
      ],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "添加失败",
          });
        }
      }
    );
  },

  //渲染标签页面
  getLabel(req, res) {
    db.dbMysql(`SELECT * FROM classify`, [], function (err, data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].classifyType == 0) {
          data[i].classifyType = true;
        } else {
          data[i].classifyType = false;
        }
        if (data[i].classifyParentID == null) {
          data[i].classifyParentID = "无";
        }
      }
      if (data) {
        res.send({
          code: 200,
          data: data,
          msg: "标签信息查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "服务器错误",
        });
      }
    });
  },

  //编辑标签
  compile(req, res) {
    db.dbMysql(
      `UPDATE classify SET classifyParentID=?, classifyType=?, classifyName=? ,classifyClass=? WHERE classifyID=?`,
      [
        req.body.classifyParentID,
        req.body.classifyType,
        req.body.classifyName,
        req.body.classifyClass,
        req.body.classifyID,
      ],
      function (err, data) {
        if (data) {
       
          res.send({
            code: 200,
            data: data,
            msg: "修改成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "修改失败",
          });
        }
      }
    );
  },

  //删除标签页面渲染
  remove(req, res) {
    db.dbMysql(
      ` DELETE FROM classify where classifyID=?`,
      [req.body.classifyID],
      function (err, data) {
        if (data) {
         
          console.log(res);
          res.send({
            code: 200,
            data: data,
            msg: "删除成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "删除失败",
          });
        }
      }
    );
  },

  //添加标签
  add(req, res) {
    db.dbMysql(
      `INSERT INTO classify(classifyID,classifyName,classifyParentID,classifyType,classifyClass) VALUES (?,?,?,?,?)`,
      [
        null,
        req.body.classifyName,
        req.body.classifyParentID,
        req.body.classifyType,
        req.body.classifyClass
      ],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "添加失败",
          });
        }
      }
    );
  },

  //渲染交易信息
  dealsDer(req, res) {
    db.dbMysql(`SELECT * FROM orderinfo`, [], function (err, data) {
      if (data) {
       
        res.send({
          code: 200,
          data: data,
          msg: "专区信息查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "服务器错误",
        });
      }
    });
  },

  //商品信息
  goods(req, res) {
    db.dbMysql(`SELECT * FROM product`, [], function (err, data) {
      if (data) {
        // console.log(data);
        res.send({
          code: 200,
          data: data,
          msg: "商品信息查询成功",
        });
      } else {
        res.send({
          code: 500,
          msg: "服务器错误",
        });
      }
    });
  },

  //商品添加
  increase(req, res) {
 
    if (req.body.productGrounding==true) {
      req.body.productGrounding=1
    }else{
      req.body.productGrounding=0
    }
    db.dbMysql(
      `INSERT INTO product(productID,productUrl,productName,productPrice,productGrounding) VALUES (?,?,?,?,?)`,
      [
        null,
        req.body.productUrl,
        req.body.productName,
        req.body.productPrice,
        req.body.productGrounding,
      ],
      function (err, data) {
        if (data) {
          res.send({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          console.log(err);
          res.send({
            code: 500,
            msg: "添加失败",
          });
        }
      }
    );
  },

  //删除商品
  omit(req, res) {
    console.log(req.body.productID);
    db.dbMysql(
      ` DELETE FROM product where productID=?`,
      [req.body.productID],
      function (err, data) {
        if (data) {
         
          console.log(res);
          res.send({
            code: 200,
            data: data,
            msg: "删除成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "删除失败",
          });
        }
      }
    );
  },

};
